﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 18th, 2013
Description: A request that processes uploading a game save as well as updating the game save dictionary.
****************************** Change Log ******************************
02.18.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameJolt.Async;
using GameJoltServices.Requests;
using GameJolt;
#endregion

namespace GameJoltServices.Saves
{
	/// <summary>
	/// 
	/// </summary>
	public sealed class UploadSaveRequest : ServiceRequestBase<GameSaveService, bool>
	{
		#region Properties
		public GameSave GameSave
		{
			get;
			set;
		}
		#endregion

		#region Constructors
		public UploadSaveRequest(GameSaveService service, GameSave gameSave)
			: base(service)
		{
			this.GameSave = gameSave;
		}
		#endregion

		#region Methods
		public override bool Process(IAsyncResult result)
		{
			var token1 = Service.UpdateSaves(GameSave.Username, GameSave.UserToken, GameSave); // update the dictionary
			var token2 = DataStore.BeginSetByKey(Service.Manager.GameID, Service.Manager.PrivateKey, GameSave.Username, GameSave.UserToken, GameSave.Location, GameSave.SaveData); // upload the data
			while(!token1.IsCompleted && !token2.IsCompleted)
			{
				System.Threading.Thread.Sleep(0);
			}

			// We store the file locally AFTER the update so that the local copy has a newer timestamp and will be used over the cloud.
			Service.CacheSave(GameSave);
			return GameJolt.RequestHelper.WasSuccessful(DataStore.EndSetByKey(token2));
		}
		#endregion
	}
}
